'''

爬虫 贴吧爬取
'''


# -*- coding: utf-8 -*-
#贴吧爬取

import requests
#import xml.etree.ElementTree as etree
#import lxml
from lxml import etree
#python3报错
# from lxml import html
# etree = lxml.html.etree

class TieBa(object):
    def __init__(self, query_string):
        self.query_string = query_string
        self.url = "https://tieba.baidu.com/f"
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/537.36(KHTML, like Gecko)"}



    def params(self):
         para = {
             "kw" : self.query_string    # 参数传到这
         }
         return para


     #1  发送请求
    def send_request(self, url, parms={}):
        response = requests.get(url, params=parms, headers=self.headers)  #print(response.content)
        #print(response)  <Response [200]>  连接成功
        return response.content


    #2  清洗数据   原本为二进制数据 b
    def parse_data(self,data,rule):
        html_data = etree.HTML(data)
        data_list = html_data.xpath(rule)  #出现null
        # html_data = etree.parse(data)
        # data_list = html_data.xpath(rule)
        print(html_data)
        print(data_list)

        return data_list


    #3  保存数据
    def save_data(self,data,image_name):
        image_path = "images/" +image_name   # 图片路径为 项目位置的images文件夹
        with open(image_path, "wb") as f:
            f.write(data)





#  主要运行逻辑
    def run(self):
        tieba_parames = self.params()
        #print(tieba_parames) {'kw': '宇宙'}
        datas = self.send_request(self.url, tieba_parames)  #接收响应的数据
        #print(datas)  # 有输出  是二进制数据b'\r\n<!DOCTYPE html>\r\n<!--STATUS OK-->\r\n<html>\r\n<head>\r\n    <meta charset="UTF-8">\r\n    <meta http
        # xpath 解析
        detail_rule = '//div[@class="t_ton cleafix"]/div/div/div/a/@href'  # 找到百度贴吧搜索美女后出现的页面   里面找到有a标签链接的
        url_list = self.parse_data(datas, detail_rule)    #


        for label in url_list:
            #加上 就是完整的链接
            detail_rule = "https://tieba.baidu.com" + label
            detail_data = self.send_request(detail_rule)  # 访问链接
            #解析图片URL
            image_url = '//img[@class="BDE_Image"]/@src'
            image_url_list = self.parse_data(detail_data,image_url)    #页面里面找到图片链接
            #print(image_url_list)

            #列表切片取图片名字
            for image_url_l in image_url_list:
                image_data = self.send_request(image_url_l)
                image_name = image_url_l[-12:] # 切后面的12位为名字
                self.save_data(image_data,image_name)



if __name__ == '__main__':
    a = input("清输入你要查询的关键字：")
    tiebas = TieBa(a)
    tiebas.run()
